<template>
  <el-card>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="角色名称" v-model="roleName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="inquiry"
          >查询</el-button
        >
        <el-button @click="clearInputValue">清空</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="showAddRole">添加角色</el-button>
    <el-button type="danger">批量删除</el-button>
    <el-table class="mt-10" border :data="roleList">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column label="角色名称" prop="roleName" width="500" />
      <el-table-column type="index" label="操作" width="300">
        <template v-slot="{ row }">
          <el-button
            :icon="UserFilled"
            type="info"
            size="small"
            @click="toAssignPermissions(row)"
          />
          <el-button :icon="Check" type="primary" size="small" />
          <el-button :icon="Edit" type="primary" size="small" />
          <el-button :icon="Delete" type="danger" size="small" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="trademark-pagination"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[4, 8, 12]"
      layout="prev, pager, next, jumper,sizes,total"
      :total="total"
      @size-change="getRoleListPage"
      @current-change="getRoleListPage"
    />
    <el-dialog v-model="dialogAddRole" title="添加角色">
      <el-form
        label-width="120px"
        ref="roleFormRef"
        :model="roleFormData"
        :rules="roleFormRules"
      >
        <el-form-item label="请输入新名称" prop="roleName">
          <el-input
            v-model="roleFormData.roleName"
            :style="{ width: '300px' }"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="confirmSaveRole">保存</el-button>
          <el-button @click="dialogAddRole = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "LRole",
};
</script>

<script lang="ts" setup>
import {
  Search,
  UserFilled,
  Edit,
  Delete,
  Check,
} from "@element-plus/icons-vue";
import { onMounted, reactive, ref } from "vue";
import type { RoleList } from "@/api/acl/model/userModel";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { getRoleListApi, getSaveRoleListApi } from "@/api/acl/role";
import type { RoleItem } from "@/api/acl/model/userModel";
import { useRouter } from "vue-router";
const router = useRouter();
const toAssignPermissions = (row: RoleItem) => {
  router.push({
    name: "RoleAuth",
    query: {
      id: row.id,
      roleName: row.roleName,
    },
  });
};
/************* 渲染分页***************/
const currentPage = ref(1);
const pageSize = ref(4);
const total = ref(0);
const roleName = ref("");
const isSearch = ref(false);
const roleList = ref<RoleList>([]);
const getRoleListPage = async () => {
  const result = await getRoleListApi(
    currentPage.value,
    pageSize.value,
    isSearch.value ? roleName.value : ""
  );
  roleList.value = result.items;
  total.value = result.total;
};
onMounted(getRoleListPage);
// 查询功能
const inquiry = () => {
  isSearch.value = true;
  getRoleListPage();
};
// 清空功能
const clearInputValue = () => {
  roleName.value = "";
};
/*********** 添加角色 **************/
const dialogAddRole = ref(false);
const showAddRole = () => {
  dialogAddRole.value = true;
  roleFormData.roleName = "";
  roleFormRef.value?.clearValidate();
};
// 表单三部曲

const roleFormRef = ref<FormInstance>();
const roleFormData = reactive({
  roleName: "",
});
const roleFormRules = reactive<FormRules>({
  roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
});
// 确认添加角色
const confirmSaveRole = async () => {
  await roleFormRef.value?.validate();
  await getSaveRoleListApi(roleFormData.roleName);
  dialogAddRole.value = false;
  ElMessage.success("添加成功");
  getRoleListPage();
};
</script>

<style scoped></style>
